package com.lantosec.hmrp.modules.archives.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.archives.entity.UserOffice;
import com.lantosec.hmrp.modules.archives.mapper.UserOfficeMapper;
import com.lantosec.hmrp.modules.archives.service.IUserOfficeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.archives.vo.UserOfficeVO;
import com.lantosec.hmrp.modules.system.service.ISysDeptService;
import com.lantosec.hmrp.modules.system.service.ISysDictionariesService;
import com.lantosec.hmrp.modules.system.service.ISysPositionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 任职信息表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-06-09
 */
@Service
@Primary
@Log4j2
public class UserOfficeServiceImpl extends ServiceImpl<UserOfficeMapper, UserOffice> implements IUserOfficeService {
    @Autowired
    private ISysDictionariesService iSysDictionariesService;

    @Autowired
    private ISysDeptService iSysDeptService;

    @Autowired
    private ISysPositionService iSysPositionService;

    @Override
    public List<UserOfficeVO> getUserOffice(String id) {
        List<UserOfficeVO> userOfficeVOList = new ArrayList<>();
        List<UserOffice> userOfficeList = this.list(new QueryWrapper<UserOffice>().eq(UserOffice.ColName.user_id.toString(),id).orderByDesc(UserOffice.ColName.create_time.toString()));
        userOfficeList.forEach(param->{
            UserOfficeVO userOfficeVO = getUserOfficeVO(param);
            userOfficeVOList.add(userOfficeVO);
        });
        return userOfficeVOList;
    }

    /**
     * 获取任职信息
     * @param userOffice
     * @return
     */
    private UserOfficeVO getUserOfficeVO(UserOffice userOffice) {
        UserOfficeVO userOfficeVO = userOffice.convertToVO();
        try {
            //查询字典表对应的职务
            String duty = userOffice.getDuty();
            String dutyName = iSysDictionariesService.getById(duty).getDictionariesShowName();
            userOfficeVO.setDutyName(dutyName);
            //查询字典表对应的职级
            String dutyLevel = userOffice.getDutyLevel();
            String dutyLevelName = iSysDictionariesService.getById(dutyLevel).getDictionariesShowName();
            userOfficeVO.setDutyLevelName(dutyLevelName);
            //查询字典表对应的人员类别
            String personType = userOffice.getPersonType();
            String personTypeName = iSysDictionariesService.getById(personType).getDictionariesShowName();
            userOfficeVO.setPersonTypeName(personTypeName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userOfficeVO;
    }

    @Override
    @Transactional
    public void saveUserOffice(UserOfficeVO vo) {
        UserOffice userOffice = vo.convertToEntity();
        userOffice.setUserOfficeId(UUIDUtils.get());
        //获取当前用户id和名称
        userOffice.setCreateId(TokenUtil.getUserId());
        userOffice.setCreateUser(TokenUtil.getName());
        userOffice.setCreateTime(DateUtils.nowDate());
        //查询部门名称
        String deptId = userOffice.getDeptId();
        String deptName = iSysDeptService.getById(deptId).getDeptName();
        userOffice.setDeptName(deptName);
        //查询岗位名称
        String[] positionIds = userOffice.getPositionId().split("-");
        String positionName = iSysPositionService.getById(positionIds[1]).getPositionName();
        userOffice.setPositionId(positionIds[1]);
        userOffice.setPositionName(positionName);
        userOffice.setVerifyStatus(1);
        this.save(userOffice);
        //新增部门人员挂接表
        baseMapper.saveDeptUser(deptId,userOffice.getUserId(),positionIds[1]);
    }

    @Override
    @Transactional
    public void updateUserOffice(UserOfficeVO vo) {
        String userOfficeId = vo.getUserOfficeId();
        //新增一条记录
        //saveUserOffice(vo);
        //修改这条数据的审核状态为2
        if(StringUtils.isNotEmpty(userOfficeId)){
            UserOffice userOffice = vo.convertToEntity();
            String[] positionIds = userOffice.getPositionId().split("-");
            userOffice.setPositionId(positionIds[1]);
            userOffice.setVerifyStatus(1);
            this.updateById(userOffice);
        }else {
            saveUserOffice(vo);
        }
    }

    @Override
    public List<UserOfficeVO> getUserOfficeVerify(String userId) {
        List<UserOfficeVO> userOfficeVOList = new ArrayList<>();
        //查询用户任职信息为未审核和审核中的
        List<UserOffice> userOfficeList = this.list(new QueryWrapper<UserOffice>().eq(UserOffice.ColName.user_id.toString(),userId).in(UserOffice.ColName.verify_status.toString(),0,2));
        userOfficeList.forEach(param->{
            UserOfficeVO userOfficeVO = getUserOfficeVO(param);
            userOfficeVOList.add(userOfficeVO);
        });
        return userOfficeVOList;
    }

    @Override
    public void verifyUserOffice(UserOfficeVO vo) {
        UserOffice userOffice = vo.convertToEntity();
        Integer verifyStatus = userOffice.getVerifyStatus();
        //如果审核不通过
        if(vo.getState() == 0){
            //审核状态为审核中的变成 已审核
            if(verifyStatus == 2){
                userOffice.setVerifyStatus(1);
            }
            //审核状态为未审核的变成 审核不通过
            if(verifyStatus == 0){
                userOffice.setVerifyStatus(3);
            }
        }
        //如果审核通过
        if(vo.getState() == 1){
            //审核状态为审核中的变为 历史记录
            if(verifyStatus == 2){
                userOffice.setVerifyStatus(4);
            }
            //审核状态为未审核的变为 已审核
            if(verifyStatus == 0){
                userOffice.setVerifyStatus(1);
            }
        }
        //获取当前用户id和名称
        userOffice.setVerifyId(TokenUtil.getUserId());
        userOffice.setVerifyUser(TokenUtil.getName());
        userOffice.setVerifyTime(DateUtils.nowDate());
        this.updateById(userOffice);
    }

    @Override
    public List<UserOfficeVO> getUserOfficeHistory(String userId) {
        List<UserOfficeVO> userOfficeList = new ArrayList<>();
        //查询用户基本信息为4历史记录的
        List<UserOffice> userOffices = this.list(new QueryWrapper<UserOffice>().eq(UserOffice.ColName.user_id.toString(),userId).eq(UserOffice.ColName.verify_status.toString(),4)
                .orderByDesc(UserOffice.ColName.create_time.toString()));
        userOffices.forEach(param->{
            UserOfficeVO userOfficeVO = getUserOfficeVO(param);
            userOfficeList.add(userOfficeVO);
        });
        return userOfficeList;
    }
}
